package game.module.mj.processor;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.mj.MjConstants;
import game.module.mj.bean.MjRound;
import game.module.mj.bean.TagWeaveItem;
import game.module.mj.logic.MjDataManager;
import game.module.mj.logic.MjLogic;
import game.module.user.bean.PlayerBean;
import game.module.user.logic.RoomDataManager;
import lion.common.ArrayUtility;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageLogin.S2ErrorCode;
import lua.LuaMessageMj.C2SMjBackRoom;
import lua.LuaMessageMj.MJHuaPai;
import lua.LuaMessageMj.MJMemberInfo;
import lua.LuaMessageMj.S2CMjBackRoom;
import lua.LuaMessageMj.WeaveItem;
import lua.LuaMessageMj.WeaveItems;
import lua.LuaMessageMj.cbCardData;

@MsgCodeAnn(msgcode = C2SMjBackRoom.id,accessLimit = 500)
public class MjBackRoomProcessor extends PlayingRoleMsgProcessor{

	private static Logger logger = LoggerFactory.getLogger(MjBackRoomProcessor.class);
	@Override
	public void process(PlayingRole playingRole, RequestMessageRaw requestMessage) throws Exception {
		int playerId = playingRole.getId();
		logger.info("mj back to room,playerId={}", playerId);
		//房间是否存在
		MjRound mjround = MjDataManager.getInstance().getMjRoom(playerId);
		if(mjround == null) {
			S2ErrorCode respMsg = new S2ErrorCode(201);
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;
		}
		List<Integer> playerIdList = mjround.getPlayerIds();
		int Chair_Id = playerIdList.indexOf(playerId);
		byte m_cbCardIndex[][] = mjround.getCardIndex();
		TagWeaveItem[][] WeaveItems = mjround.getM_WeaveItemArray();
		//do
		boolean single[] = mjround.getSingGold();
		boolean shuanggold[] = mjround.getDoubleGold();
		boolean threegold[] = mjround.getThreeGold();
		
			
		S2CMjBackRoom respMsg = new S2CMjBackRoom();
		if(single[Chair_Id] || shuanggold[Chair_Id] || threegold[Chair_Id])
			respMsg.gold_state=1;
		else
			respMsg.gold_state=0;
		respMsg.room_id = mjround.getRoomUuid();
		respMsg.round_count = mjround.getRound_count();
		respMsg.current_round = mjround.getRoundSum();
		if(mjround.getCreate_time()!=0)
		    respMsg.create_time = mjround.getCreate_time();
		if(mjround.getM_lGameScore()!=null)
	    	respMsg.score = mjround.getM_lGameScore();
		respMsg.zhuangjia_index = (byte)mjround.getM_wBankerUser();
		respMsg.chupaiIndex = (byte) mjround.getM_wCurrentUser();
		if(mjround.getKaijin_saizi()!=null)
		    respMsg.kaijinsazi =ArrayUtility.toShortArray(mjround.getKaijin_saizi());
		respMsg.Buhua_num = mjround.getBuhua_count();
		respMsg.members = new MJMemberInfo[mjround.getPlayerIds().size()];
		respMsg.init_huacount = mjround.getInit_hua();
		int i = 0;
		for(Integer aPlayer:playerIdList) {
			PlayerBean poc = RoomDataManager.getInstance().getPlayerBean(aPlayer);
			respMsg.members[i++] = new MJMemberInfo(aPlayer, poc.getHeadimg(), 1, poc.getSex().shortValue(),
					poc.getName());
		}
		byte card_data[] =new byte[18];
		MjLogic.SwitchToCardData(m_cbCardIndex[Chair_Id], card_data);
		
		respMsg.my_cards = card_data;
		
		WeaveItems[] allplayItems = new WeaveItems[4];
		for(int l = 0;l<allplayItems.length;l++) {
			allplayItems[l] = new WeaveItems();
		}
		int player = 0;
		for(TagWeaveItem[] aplayWeaveItems:WeaveItems) {
			int weave_index = 0;
			for(TagWeaveItem aWeaveItems:aplayWeaveItems) {
				if(aWeaveItems.cbWeaveKind==0)
					continue;
				weave_index++;
			}
			allplayItems[player].aplayer_WeaveItem = new  WeaveItem[weave_index]; //每一个玩家有最大5个组合
			for(int k =0;k<weave_index;k++) {
				allplayItems[player].aplayer_WeaveItem[k] = new WeaveItem();
			}
			player++;
		}
		
		int player_index=0;
		for(TagWeaveItem[] aplayWeaveItems:WeaveItems) {
			int weave_index = 0;
			for(TagWeaveItem aWeaveItems:aplayWeaveItems) {
				if(aWeaveItems.cbWeaveKind==0)
					continue;
				allplayItems[player_index].aplayer_WeaveItem[weave_index].cbWeaveKind = aWeaveItems.cbWeaveKind; //组合类型
				allplayItems[player_index].aplayer_WeaveItem[weave_index].cbCenterCard = aWeaveItems.cbCenterCard; //中心扑克
				allplayItems[player_index].aplayer_WeaveItem[weave_index].cbPublicCard = aWeaveItems.cbPublicCard; //公开的标志
				allplayItems[player_index].aplayer_WeaveItem[weave_index].wProvideUser = (byte) aWeaveItems.wProvideUser; //提供的用户
				allplayItems[player_index].aplayer_WeaveItem[weave_index].cbCardData = (byte[]) aWeaveItems.cbCardData; //拍值
				weave_index++;
			}
			player_index++;
		}
		respMsg.playWeaveItem = allplayItems; //所有组合
		List<List<Byte>> huapai = mjround.getH_cards();//花牌
		MJHuaPai[] hua = new MJHuaPai[4];
		int hua_index = 0;
		for(List<Byte> aplayhua :huapai) {
			hua[hua_index] = new MJHuaPai();
			hua[hua_index].cards = ArrayUtility.toByteArray(aplayhua);
			hua_index++;
		}
		respMsg.huapais = hua;
		byte[] playCardNum = new byte[4];
		for(int playsize=0;playsize<playCardNum.length;playsize++) {
			playCardNum[playsize] = MjLogic.GetCardCount(m_cbCardIndex[playsize]);
		}
		respMsg.PlayerCardsNum = playCardNum;
		List<Byte> remain_card = mjround.getRemain_card();
		respMsg.Card_num = (short) remain_card.size();
		respMsg.god_card = mjround.getGod_card();
		cbCardData aplay[] = new cbCardData[4];
		List<List<Byte>> pushed_cards = mjround.getPushed_card();
		int cards_index = 0;
		for(List<Byte> aplayer :pushed_cards) {
			aplay[cards_index] = new cbCardData();
			aplay[cards_index].cards = ArrayUtility.toByteArray(aplayer);
			cards_index++;
		}
		respMsg.pushedcards = aplay;
		respMsg.bankuser_score = (short) (mjround.getM_cbLianZhuangCount()*MjConstants.Zhuang_Score);
		respMsg.action = mjround.getM_cbUserAction()[Chair_Id];
		if(respMsg.action>0) {
			respMsg.action_card= (byte) mjround.getM_cbProvideCard();
		}
		respMsg.last_play = (byte) mjround.getM_wProvideUser();
		respMsg.wait_game_start = mjround.getWait_game();
		respMsg.cost_type = mjround.getCost_way();
		byte ready[] = new byte[4];
		boolean state[] = mjround.getPlayReady();
		for(int a=0;a<state.length;a++) {
			if(state[a])
				ready[a]=1;
			else 
				ready[a]=0;
		}
		respMsg.ready_state = ready;
		playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
		//respMsg.my_cards = MjLogic.SwitchToCardIndex(cbCardData, cbCardCount, cbCardIndex);
				
	}
    
	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {
		
	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {
		
	}

}
